<script>
	import {
		mapMutations
	} from 'vuex'
	import {
		useCartStore
	} from '@/store/cart.js'
	import {
		version
	} from './package.json'
	import amap from '@/common/amap-wx.130.js';
	import store from '@/store/index.js';
	import {
		ref
	} from 'vue'
	import {
		mapwxkey
	} from '@/utils/domain.js'
	import {
		queryCityCode,
	} from '@/api/common.js'
	import {
		initEid
	} from './mp_ecard_sdk/main';

	// #ifdef APP
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
	// #endif
	export default {
		onLaunch: async function() {
			const isFirstOpen = !uni.getStorageSync('isFirstOpen');
			if (isFirstOpen) {
				uni.setStorageSync('isFirstOpen', true);
				uni.redirectTo({
					url: '/pages/login/swiperPages'
				});
				return; // 直接返回，避免后续逻辑干扰
			}

			// #ifdef H5
			console.log(
				`%c hello uniapp %c v${version} `,
				'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
				'background:#007aff ;padding: 1px; border-radius: 0 3px 3px 0;  color: #fff; font-weight: bold;'
			)
			// #endif
			console.log('App Launch');

			// #ifdef APP-PLUS
			// App平台检测升级（保留）
			if (plus.runtime.appid !== 'HBuilder') {
				checkUpdate()
			}
			// 一键登录预登陆（保留）
			uni.preLogin({
				provider: 'univerify',
				success: (res) => {
					this.setUniverifyErrorMsg();
					console.log("preLogin success: ", res);
				},
				fail: (res) => {
					this.setUniverifyLogin(false);
					this.setUniverifyErrorMsg(res.errMsg);
					console.log("preLogin fail res: ", res);
				}
			})
			// #endif
			initEid(); // 初始化EID（保留）

			// 初始化登录状态（保留，但不强制跳转）
			await store.dispatch('initializeApp');
			// 初始化购物车数量（保留）
			const cartStore = useCartStore()
			cartStore.initCartCount()


		},
		onShow: function() {
			console.log('App Show')
			const amapPlugin = new amap.AMapWX({
				key: mapwxkey
			});
			amapPlugin.getRegeo({

				success: (data) => {
					console.log(data, '地图自身定位');
					queryCityCode({
						longitude: data[0].longitude,
						latitude: data[0].latitude
					}).then(res => {
						// console.log('queryCityCode 接口返回', res);
						res.data.areaId = res.data.areaCode; // 补充 areaId 字段
						// 保存当前定位信息到 Vuex
						this.setCurrentLocation(res.data);

					}).catch(err => {
						console.error('queryCityCode 接口请求失败', err);
					});
				},
				fail(e) {
					console.error('地图定位失败', e);
				}
			});
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			test: ''
		},
		methods: {
			...mapMutations(['setUniverifyErrorMsg', 'setUniverifyLogin', 'setCurrentLocation'])
		}
	}
</script>

<style lang="scss">
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-PLUS-NVUE */
	@import './common/uni.css';
	@import '@/static/customicons.css';

	/* #ifdef H5 */
	@media screen and (min-width: 768px) {
		body {
			overflow-y: scroll;
		}
	}

	uni-page-body {
		background-color: #F5F5F5 !important;
		min-height: 100% !important;
		height: auto !important;
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #fff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}

	/* #endif */

	page {
		// background-color: #efeff4;
		// height: 100%;
		// font-size: 28rpx;
		/* line-height: 1.8; */
	}

	.fix-pc-padding {
		padding: 0 50px;
	}

	.uni-header-logo {
		padding: 30rpx;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 10rpx;
	}

	.uni-header-image {
		width: 100px;
		height: 100px;
	}

	.uni-hello-text {
		color: #7A7E83;
	}

	.uni-hello-addfile {
		text-align: center;
		line-height: 300rpx;
		background: #FFF;
		padding: 50rpx;
		margin-top: 10px;
		font-size: 38rpx;
		color: #808080;
	}

	/* #endif*/
</style>